package com.jeesite.modules.ch.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jeesite.common.web.http.HttpClientUtils;
import com.jeesite.modules.ch.dao.ChMyTaskDao;
import com.jeesite.modules.ch.entity.ChMyTask;
import com.jeesite.modules.ch.entity.param.MyTaskStatus;
import com.jeesite.modules.ch.service.ChLiushuiService;
import com.jeesite.modules.ch.service.ChMyTaskService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.checkerframework.checker.units.qual.C;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.ch.entity.ChChannel;
import com.jeesite.modules.ch.service.ChChannelService;

/**
 * ch_channelController
 * @author 李寒冰
 * @version 2025-08-27
 */
@Controller
@RequestMapping(value = "${adminPath}/ch/chChannel")
public class ChChannelController extends BaseController {

	@Autowired
	private ChChannelService chChannelService;
    @Autowired
    private ChLiushuiService chLiushuiService;
    @Autowired
    private ChMyTaskDao chMyTaskDao;
    @Autowired
    private ChMyTaskService chMyTaskService;

	@Value("${pic.reqUrl}")
	private String picReqUrl;

	/**
	 * 获取数据
	 */
	@ModelAttribute
	public ChChannel get(String id, boolean isNewRecord) {
		return chChannelService.get(id, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("ch:chChannel:view")
	@RequestMapping(value = {"list", ""})
	public String list(ChChannel chChannel, Model model) {
		model.addAttribute("chChannel", chChannel);
		return "modules/ch/chChannelList";
	}
	
	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("ch:chChannel:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<ChChannel> listData(ChChannel chChannel, HttpServletRequest request, HttpServletResponse response) {
		chChannel.setPage(new Page<>(request, response));
		Page<ChChannel> page = chChannelService.findPage(chChannel);
		List<ChChannel> chChannelList = page.getList();
		for (ChChannel chChannel2 : chChannelList) {
			String id = chChannel2.getId();
			String appId = chChannel2.getAppId();
			//做单流水
			ChMyTask chMyTask = new ChMyTask();
			chMyTask.setChannelId(appId);
			double sumLiushui = chMyTaskDao.liushuiTask(chMyTask);
			chChannel2.setLiushuOver(sumLiushui);
			//余额
			double yue = chLiushuiService.yue(id);
			chChannel2.setYue(yue);
			//总任务数
			ChMyTask chMyTask2 = new ChMyTask();
			chMyTask2.setChannelId(appId);
			chMyTask2.setMyTaskStatus(MyTaskStatus.YITONGGUO.getCode());
			long count = chMyTaskDao.findCount(chMyTask2);
			chChannel2.setOverNum(new Long(count).intValue());
		}
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("ch:chChannel:view")
	@RequestMapping(value = "form")
	public String form(ChChannel chChannel, Model model) {
		model.addAttribute("chChannel", chChannel);
		return "modules/ch/chChannelForm";
	}

	/**
	 * 保存数据
	 */
	@RequiresPermissions("ch:chChannel:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated ChChannel chChannel) {
		try {
			chChannelService.save(chChannel);
		}catch (RuntimeException e){
			return renderResult(Global.FALSE, text(e.getMessage()));
		}
		//渠道信息同步到接口
		Map<String,String> map = new HashMap();
		map.put("id",chChannel.getId());
		String post = HttpClientUtils.post(picReqUrl + "/api/channelAdmin/gengxinChannelCache", map);

		return renderResult(Global.TRUE, text("保存ch_channel成功！"));
	}
	
	/**
	 * 删除数据
	 */
	@RequiresPermissions("ch:chChannel:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(ChChannel chChannel) {
		chChannel = chChannelService.get(chChannel);
		String appId = chChannel.getAppId();
		ChMyTask chMyTask = new ChMyTask();
		chMyTask.setChannelId(appId);
		long count = chMyTaskService.findCount(chMyTask);
		if (count > 0) {
			return renderResult(Global.FALSE, text("当前渠道已有任务数据，不可删除！"));
		}
		chChannelService.delete(chChannel);

		return renderResult(Global.TRUE, text("删除ch_channel成功！"));
	}
	
}